package com.buaa.divinationmaster.database.dao;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.buaa.divinationmaster.database.entity.DivinationRecordEntity;

import java.util.List;

/**
 * 占卜记录数据访问对象
 */
@Dao
public interface DivinationRecordDao {
    
    @Insert
    long insert(DivinationRecordEntity record);
    
    @Update
    void update(DivinationRecordEntity record);
    
    @Delete
    void delete(DivinationRecordEntity record);
    
    @Query("SELECT * FROM divination_history ORDER BY timestamp DESC")
    List<DivinationRecordEntity> getAllRecords();
    
    @Query("SELECT * FROM divination_history WHERE id = :id")
    DivinationRecordEntity getRecordById(long id);
    
    @Query("SELECT * FROM divination_history WHERE type = :type ORDER BY timestamp DESC")
    List<DivinationRecordEntity> getRecordsByType(String type);
    
    @Query("SELECT * FROM divination_history WHERE isFavorite = 1 ORDER BY timestamp DESC")
    List<DivinationRecordEntity> getFavoriteRecords();
    
    @Query("SELECT * FROM divination_history WHERE question LIKE '%' || :keyword || '%' ORDER BY timestamp DESC")
    List<DivinationRecordEntity> searchRecords(String keyword);
    
    @Query("SELECT * FROM divination_history ORDER BY timestamp DESC LIMIT :limit OFFSET :offset")
    List<DivinationRecordEntity> getRecordsPaged(int limit, int offset);
    
    @Query("SELECT COUNT(*) FROM divination_history")
    int getRecordCount();
    
    @Query("SELECT COUNT(*) FROM divination_history WHERE type = :type")
    int getRecordCountByType(String type);
    
    @Query("DELETE FROM divination_history")
    void deleteAllRecords();
    
    @Query("DELETE FROM divination_history WHERE timestamp < :timestamp")
    void deleteOldRecords(long timestamp);
}
